package com.example.makerspace.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.makerspace.dao.*;
import com.example.makerspace.domain.*;
import com.example.makerspace.dto.*;
import com.example.makerspace.enums.ApplicationStatusEnum;
import com.example.makerspace.enums.ResultCodeEnum;
import com.example.makerspace.service.CompetitionService;
import com.example.makerspace.utils.JwtUtils;
import com.example.makerspace.utils.ValidateCodeUtils;
import com.example.makerspace.vo.ProcessStatusVO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.swing.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class CompetitionServiceImp implements CompetitionService {

    @Resource
    private UserCompetitionRelationShipDAO userCompetitionRelationShipDAO;
    @Resource
    private CompetitionDAO competitionDAO;
    @Resource
    private ApplicationV2DAO applicationV2DAO;

    @Resource
    private QuestionsCategoryDAO questionsCategoryDAO;

    @Resource
    private QuestionTypeDAO questionTypeDAO;

    @Resource
    private  CompetitionChangeRecordDAO competitionChangeRecordDAO;

    @Resource
    private  CompetitionSubmissionDAO competitionSubmissionDAO;

    @Override
    public Result checkCompetition(HttpServletRequest request, Integer id) {
        String username = JwtUtils.getUsername(request);
        ApplicationV2DO applicationV2DO = applicationV2DAO.selectByUsername(username);
        if (applicationV2DO == null) {
            return Result.error(ResultCodeEnum.USER_NO_Application);
        }
        if (applicationV2DO.getStatus().equals(ApplicationStatusEnum.APPLICATION_SUCCESS.getCode())) {
            UserCompetitionRelationShipDO userCompetitionRelationShipDO = userCompetitionRelationShipDAO.checkCompetition(username, id);
            if (userCompetitionRelationShipDO == null) {
                return Result.success("用户还没有进行选择改赛题");
            }
            return Result.success(applicationV2DO);
        }
        return  Result.error(ResultCodeEnum.USER_NO_PASS);
    }


    @Override
    public Result save(CompetitionDTO competitionDTO, String username) {
        CompetitionDO competitionDO = new CompetitionDO();
        UserCompetitionRelationShipDO userCompetitionRelationShipDO = new UserCompetitionRelationShipDO();
        userCompetitionRelationShipDO.setUsername(username);
        userCompetitionRelationShipDO.setCompetitionId(competitionDTO.getId());
        userCompetitionRelationShipDO.setState("审核中");
        BeanUtils.copyProperties(competitionDTO,competitionDO);
        competitionDAO.insert(competitionDO);
        userCompetitionRelationShipDAO.insert(userCompetitionRelationShipDO);
        return  Result.success(ApplicationStatusEnum.APPLICATION_SUCCESS);
    }
    @Override
    public IPage<CompetitionDO> selectByCondition(PageRequest pageRequest) {
        Pagination pagination = pageRequest.getPagination();
        Integer competitionParentId = pageRequest.getCompetitionParentId();
        Map<String, Object> params = pageRequest.getParams();
        if(params.size()==0){
            return competitionDAO.selectByCompetitionParentId(pagination,competitionParentId);
        }
        String queryParam = MapUtils.getString(params, "queryParam");
        return competitionDAO.selectByCondition(pagination,queryParam,competitionParentId);
    }
    @Override
    public  IPage<CompetitionDO>  designateQuery(PageRequest pageRequest) {
        Pagination pagination = pageRequest.getPagination();
        String type = pageRequest.getType();
        int typeInt = Integer.parseInt(type);
        if(typeInt == 0){
            return  competitionDAO.selectByCompetitionParentId(pagination,pageRequest.getCompetitionParentId());
        }
        return  competitionDAO.selectByType(pagination,typeInt, pageRequest.getCompetitionParentId());
    }

    @Override
    public Result saveQuestionsCategory(CompetitionCategoryDTO competitionCategoryDTO) {
        CompetitionDO competitionDO = new CompetitionDO();
        BeanUtils.copyProperties(competitionCategoryDTO,competitionDO);
        QuestionsCategoryDO questionsCategoryDO = new QuestionsCategoryDO();
        questionsCategoryDO.setType(competitionCategoryDTO.getType());
        List<CompetitionDO> competitionDOList = competitionDAO.selectAll();
        String sid = ValidateCodeUtils.fillString(competitionDOList.size(), 4);
        competitionDO.setSortId(sid);
        competitionDAO.insert(competitionDO);
        questionsCategoryDAO.insert(questionsCategoryDO);
        return Result.success(ResultCodeEnum.ADD_Questions_SUCEESS);
    }

    @Override
    public Result updateQuestionsCategory(CompetitionCategoryDTO competitionCategoryDTO) {
        CompetitionDO competitionDO = new CompetitionDO();
        BeanUtils.copyProperties(competitionCategoryDTO,competitionDO);
        competitionDAO.updateById(competitionDO);
        return Result.success(ResultCodeEnum.Update_Questions_SUCEESS);
    }

    @Override
    public Result deleteQuestionsCategory(CompetitionCategoryDTO competitionCategoryDTO) {
        competitionDAO.deleteById(competitionCategoryDTO.getId());
        return  Result.success(ResultCodeEnum.DELETE_Questions_SUCEESS);
    }

    @Override
    public Result competitionChangeRecord(HttpServletRequest request, Integer competitionId) {
        String username = JwtUtils.getUsername(request);
        List<UserCompetitionRelationShipDO> userCompetitionRelationShipDOList = userCompetitionRelationShipDAO.selectByCompetitionId(username, competitionId);
        if(CollectionUtils.isEmpty(userCompetitionRelationShipDOList)){
            return  Result.error(ResultCodeEnum.Other_Error);
        }
        UserCompetitionRelationShipDO userCompetitionRelationShipDO = userCompetitionRelationShipDOList.get(0);
        CompetitionChangeRecordDO competitionChangeRecordDO = new CompetitionChangeRecordDO();
        competitionChangeRecordDO.setState(userCompetitionRelationShipDO.getState());
        competitionChangeRecordDO.setCompetitionId(competitionId);
        competitionChangeRecordDO.setUsername(username);
        List<CompetitionSubmissionDO> competitionSubmissionDOList = competitionSubmissionDAO.selectByUsernameAndId(username, competitionId);
        if(CollectionUtils.isEmpty(competitionSubmissionDOList)){
            competitionChangeRecordDO.setOperate("提交");
            competitionChangeRecordDAO.insert(competitionChangeRecordDO);
            return  Result.success(competitionChangeRecordDO);
        }
        competitionChangeRecordDO.setOperate("修改");
        competitionChangeRecordDAO.insert(competitionChangeRecordDO);

        return Result.success(competitionChangeRecordDO);
    }

    @Override
    public Result showQuestionsType() {
        List<QuestionsTypeDO> questionsTypeDOS = questionTypeDAO.selectAll();
        return  Result.success(questionTypeDAO.selectAll());
    }

    @Override
    public Result updateQuestionsType(QuestionsTypeDO questionsTypeDO) {
        return Result.success(questionTypeDAO.updateById(questionsTypeDO));
    }

    @Override
    public Result deleteQuestionsType(QuestionsTypeDO questionsTypeDO) {
        return Result.success(questionTypeDAO.deleteById(questionsTypeDO.getId()));
    }

    @Override
    public Result addQuestionsType(QuestionsTypeDO questionsTypeDO) {
        return Result.success(questionTypeDAO.insert(questionsTypeDO));
    }

    @Override
    public Result questionsProcessStatus(HttpServletRequest request, Integer competitionId) {
        String username = JwtUtils.getUsername(request);
        ProcessStatusVO processStatusVO = new ProcessStatusVO();
        List<UserCompetitionRelationShipDO> userCompetitionRelationShipDOList = userCompetitionRelationShipDAO.selectByCompetitionId(username, competitionId);
        if(CollectionUtils.isEmpty(userCompetitionRelationShipDOList)){
            return  Result.success(ResultCodeEnum.NO_SELECT_QUerstion);
        }
        UserCompetitionRelationShipDO userCompetitionRelationShipDO = userCompetitionRelationShipDOList.get(0);
        String reviewFeedback = userCompetitionRelationShipDO.getReviewFeedback();
        processStatusVO.setState(userCompetitionRelationShipDO.getState());
        processStatusVO.setReviewFeedback(reviewFeedback);
        return  Result.success(ResultCodeEnum.Question_Selection,processStatusVO);
    }

    @Override
    public Result selectCompetitionChangeRecord(Integer competitionId,String username) {
        List<CompetitionChangeRecordDO> competitionChangeRecordDOList=competitionChangeRecordDAO.selectByCompetitionIdAndUsername(competitionId,username);
        return Result.success(competitionChangeRecordDOList);
    }
}
